package cn.itrip.biz.controller;


import cn.itrip.beans.dtos.Dto;
import cn.itrip.beans.dtos.ErrorCode;
import cn.itrip.beans.dtos.InputDto;
import cn.itrip.beans.pojo.ItripUser;
import cn.itrip.beans.pojo.ItripUserLinkUser;
import cn.itrip.beans.vo.tokentakevo.UserAndLinkUserVo;
import cn.itrip.biz.service.IItripUserLinkUserService;
import cn.itrip.common.ValidationToken;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;


/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 刘怀瑜
 * @since 2020-02-27
 */
@RequestMapping("/biz/api/userinfo")
@CrossOrigin
@Api(tags = "查询常用旅客信息")
@RestController
public class ItripUserLinkUserController {
    @Resource
    private IItripUserLinkUserService iUserLinkUserService;

    @Resource
    private RedisTemplate redisTemplate;

    @ApiOperation(value = "新增旅客信息", httpMethod = "Post", protocols = "HTTP", produces = "application/json;charset=utf-8", response = InputDto.class, notes = "新增常用旅客信息")
    @PostMapping(value = "/adduserlinkuser", produces = "application/json;charset=utf-8")
    public Dto addUserLinkUser(@ApiParam(name = "itripUserLinkUser", value = "新增", required = true) @RequestBody
                                       ItripUserLinkUser itripUserLinkUser, HttpServletRequest request) {
        String token = request.getHeader("token");
        if ("".equals(token)) {
            return InputDto.error(ErrorCode.ADD_THE_ERROR);
        }

        ValidationToken validationToken = new ValidationToken();
        ItripUser user = validationToken.getToken(token, redisTemplate);
        itripUserLinkUser.setCreatedBy(user.getId());
        Integer getId = user.getId().intValue();
        itripUserLinkUser.setUserId(getId);
        int sfz = iUserLinkUserService.sfz(itripUserLinkUser);
        if (sfz > 0) {
            return InputDto.error(ErrorCode.ADD_THE_ERROR);
        } else {
            iUserLinkUserService.addAddUserLinkUser(itripUserLinkUser);
        }
        return InputDto.data("用户信息添加成功!");
    }

    @ApiOperation(value = "删除旅客信息", httpMethod = "Get", protocols = "HTTP", produces = "application/json;charset=utf-8", response = Dto.class, notes = "删除常用旅客信息")
    @GetMapping(value = "/deluserlinkuser", produces = "application/json;charset=utf-8")
    public Dto delUserLinkUser(@ApiParam(name = "ids", value = "旅客ID", required = true)
                               @RequestParam String ids) {
        if (ids == null || "".equals(ids.trim())) {
            return InputDto.error("参数不识别");
        } else {
            List<String> list = Arrays.asList(ids.split(","));
            for (String s : list) {
                Integer count = this.iUserLinkUserService.deDelUserLinkUser(s);
            }
            return InputDto.mes("删除成功");
        }
    }

    @ApiOperation(value = "修改旅客信息", httpMethod = "Post", protocols = "HTTP", produces = "application/json;charset=utf-8", response = Dto.class, notes = "修改常用旅客信息")
    @PostMapping(value = "/modifyuserlinkuser", produces = "application/json;charset=utf-8")
    public Dto modifyUserLinUser(
            @ApiParam(name = "itripUserLinkUser", value = "修改", required = true)
            @RequestBody ItripUserLinkUser itripUserLinkUser, HttpServletRequest request) {
        String token = request.getHeader("token");
        if ("".equals(token)) {
            return InputDto.error("修改人信息获取失败!");
        }
        ValidationToken validationToken = new ValidationToken();
        ItripUser user = validationToken.getToken(token, redisTemplate);
        itripUserLinkUser.setModifiedBy(user.getId());
        int sfz = iUserLinkUserService.sfz(itripUserLinkUser);
        if (sfz > 0) {
            return InputDto.error("修改失败，身份证重复，重新添加  ");

        } else {
            iUserLinkUserService.editModifyUserLinkUser(itripUserLinkUser);
        }
        return InputDto.ok();
    }

    @ApiOperation(value = "查询住客信息", httpMethod = "Post", protocols = "HTTP", produces = "application/json;charset=utf-8", response = InputDto.class, notes = "住客信息")
    @PostMapping(value = "/queryuserlinkuser", produces = "application/json;charset=utf-8")
    public Dto listQueryUserLinkUser(@ApiParam(name = "itripHotelOrder", value = "住客信息", required = true) @RequestBody UserAndLinkUserVo userAndLinkUserVo,
                                     HttpServletRequest request) {
        //拿到token里面的登陆id
        String token = request.getHeader("token");
        ValidationToken validationToken = new ValidationToken();
        ItripUser user = validationToken.getToken(token, redisTemplate);
        userAndLinkUserVo.setTokenId(user.getId());
        if (userAndLinkUserVo.getLinkUserName() == "") {
            userAndLinkUserVo.setLinkUserName(null);
        }


        List<UserAndLinkUserVo> linkUsers = iUserLinkUserService.listOrderLinkUser(userAndLinkUserVo);

        if (linkUsers != null) {
            return InputDto.data(linkUsers);
        }
        return InputDto.error(ErrorCode.QUERY_ERROR);
    }
}
